$().ready(function() {
	loadGroup();
	loadsupplierId();
	loadUserId();
	loadtaxRate();
	validateRule();
	laydate({
		elem : '#invoiceTime'
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/fin/receiptTick/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			tickAmount : {
				required : true
			},
			taxRate : {
				required : true
			}
		},
		messages : {
			tickAmount : {
				required : icon + "请输入票额"
			},
			taxRate : {
				required : icon + "请输入税率"
			}
		}
	})
}

//批次信息
function loadGroup(){
	var html = "";
	$.ajax({
		url : '/pur/group/getlistByPayment',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].groupid + '</option>'
			}
			$(".chosen-select-purId").append(html);
			$(".chosen-select-purId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-purId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				$.ajax({
					url :"/pur/group/getGroup/"+params.selected,
					data : null,
					type:"get",
					success : function(data) {
						$("input[name='supplierId']").val(data.supplierId);
						$("input[name='supplierName']").val(data.supplierName);
					}
				});
			});
		}
	});
}

//供应商信息
function loadsupplierId(){
	var html = "";
	$.ajax({
		url : '/pro/supplier/supplierlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-supplierId").append(html);
			$(".chosen-select-supplierId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-supplierId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

//收票人
function loadUserId(){
	var html = "";
	$.ajax({
		url : '/sys/user/getAlllist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].userId + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-invoiceUserId").append(html);
			$(".chosen-select-invoiceUserId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-invoiceUserId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function loadtaxRate(){
	var html = "";
	$.ajax({
		url : '/fin/reimburseType/reimtypelist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].taxRate + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-taxRate").append(html);
			$(".chosen-select-taxRate").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-taxRate').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}